﻿using System;
using System.Collections.Generic;
using System.Windows;
using BingleMaps.Extensions;
using BingleMaps.ViewModels;
using GalaSoft.MvvmLight.Command;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps.Platform;
using Serialization;

namespace BingleMaps.Models
{
	public class UserPin : ViewModelBase
	{
		[DoNotSerialize]
		public RelayCommand<RoutedEventArgs> EditCommand;

		public UserPin()
		{
			EditCommand = new RelayCommand<RoutedEventArgs>(e =>
			{
				var root = Application.Current.RootVisual as PhoneApplicationFrame;
				ViewModelLocator.MainStatic.EditingUserPin = this;
				root.Navigate(new Uri(Application.Current.Resources["EditPage"].ToString(), UriKind.Relative));
			});
		}

		[DoNotSerialize]
		private bool _isChecked;
		[DoNotSerialize]
		public bool IsChecked
		{
			get { return _isChecked; }
			set
			{
				if (value == _isChecked) return;
				_isChecked = value;
				RaisePropertyChanged("IsChecked");
			}
		}

		public Guid Key { get; set; }

		private Location _location;
		public Location Location
		{
			get { return _location; }
			set
			{
				if (value == null || value == _location) return;
				_location = value;
				RaisePropertyChanged("Location");
			}
		}

		private bool _isTemporary;
		public bool IsTemporary
		{
			get { return _isTemporary; }
			set
			{
				if (value == _isTemporary) return;
				_isTemporary = value;
				RaisePropertyChanged("IsTemporary");
			}
		}

		[DoNotSerialize]
		public IEnumerable<char> FriendlyCoordinates { get { return Location.ToLatLngString(); } }

		private string _note;
		public string Note
		{
			get { return _note; }
			set
			{
				if (value == _note) return;
				_note = value;
				RaisePropertyChanged("Note");
			}
		}

		private string _image;
		public string Image
		{
			get { return _image; }
			set
			{
				if (value == _image) return;
				_image = value;
				RaisePropertyChanged("Image");
			}
		}

		private string _color = SettingsViewModel.DefaultPushpinColor.Value;
		public string Color
		{
			get
			{
				return _color;
			}
			set
			{
				_color = value;
				RaisePropertyChanged("Color");
			}
		}

		[DoNotSerialize]
		private bool _isMoving;
		[DoNotSerialize]
		public bool IsMoving
		{
			get { return _isMoving; }
			set
			{
				if (value == _isMoving) return;
				_isMoving = value;
				RaisePropertyChanged("IsMoving");
			}
		}

		private bool _isEditing;
		public bool IsEditing
		{
			get { return _isEditing; }
			set
			{
				if (value == _isEditing) return;
				_isEditing = value;
				RaisePropertyChanged("IsEditing");
			}
		}

		private string _title = "my pin";
		public string Title
		{
			get { return _title; }
			set
			{
				if (value == _title) return;
				_title = value;
				RaisePropertyChanged("Title");
			}
		}

		private string _address;
		public string Address
		{
			get { return _address; }
			set
			{
				if (value == _address) return;
				_address = value;
				RaisePropertyChanged("Address");
				Display = value ?? Location.ToLatLngString();
			}
		}

		[DoNotSerialize]
		private string _display;
		[DoNotSerialize]
		public string Display
		{
			get { return _display; }
			set
			{
				if (value == _display) return;
				_display = value;
				RaisePropertyChanged("Display");
			}
		}

		private string _icon = SettingsViewModel.DefaultPushpinIcon.Value;
		public string Icon
		{
			get
			{
				return _icon;
			}
			set
			{
				if (value == _icon) return;
				_icon = value;
				RaisePropertyChanged("Icon");
			}
		}
	}
}
